//
//  AppDelegate.m
//  MK
//
//  Created by Le Quang Vinh on 8/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    // tabBarController
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.delegate = self;
    
    // homeViewController
    homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    homeViewController.tabBarItem.title = @"Home";
    //homeViewController.tabBarItem.image = [UIImage imageNamed:@"flag1.png"];
    UINavigationController *ncHome = [[UINavigationController alloc] initWithRootViewController:homeViewController];
    ncHome.delegate = self;
    [homeViewController release];
    
    // socialViewController
    socialViewController = [[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil];
    socialViewController.tabBarItem.title = @"Social";
    UINavigationController *ncSocial = [[UINavigationController alloc] initWithRootViewController:socialViewController];
    ncSocial.delegate = self;
    [socialViewController release];
    
    // newsViewController
    newsViewController = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:nil];
    newsViewController.tabBarItem.title = @"News";
    UINavigationController *ncNews = [[UINavigationController alloc] initWithRootViewController:newsViewController];
    ncNews.delegate = self;
    [newsViewController release];
    
    // musicViewController
    musicViewController = [[MusicViewController alloc] initWithNibName:@"MusicViewController" bundle:nil];
    musicViewController.tabBarItem.title = @"Music";
    UINavigationController *ncMusic = [[UINavigationController alloc] initWithRootViewController:musicViewController];
    ncMusic.delegate = self;
    [musicViewController release];
    
    // linksViewController
    linksViewController = [[LinksViewController alloc] initWithNibName:@"LinksViewController" bundle:nil];
    linksViewController.tabBarItem.title = @"Links";
    UINavigationController *ncLinks = [[UINavigationController alloc] initWithRootViewController:linksViewController];
    ncLinks.delegate = self;
    [linksViewController release];
    
    infoViewController = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
    infoViewController.tabBarItem.title = @"Info";
    UINavigationController *ncInfo = [[UINavigationController alloc] initWithRootViewController:infoViewController];
    ncInfo.delegate = self;
    [infoViewController release];
    
    //    tabBarController.viewControllers = controllers;
    //    tabBarController.customizableViewControllers = controllers;
    //    [tabBarController setSelectedViewController:homeNavigationController];
    //    [controllers release];
    //    tabBarController.delegate = self;
    
    tabBarController.viewControllers = [NSArray arrayWithObjects:ncHome, ncSocial, ncNews, ncMusic, ncLinks, ncInfo, nil];
    tabBarController.moreNavigationController.navigationBar.barStyle = UIBarStyleDefault;
    tabBarController.customizableViewControllers = nil;
    //tabBarController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:0];
    [ncHome release];
    [ncSocial release];
    [ncNews release];
    [ncMusic release];
    [ncLinks release];
    [ncInfo release];
    
    //tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    //[self presentModalViewController:tabBarController animated:YES];
    
    //[self.window addSubview:tabBarController.view];
    
    MainScreenCtrl* ctrl = [[MainScreenCtrl alloc] initWithNibName:@"MainScreenCtrl" bundle:nil];
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:ctrl];
    UIView *view = ctrl.view;
    CGRect frame = view.frame;
    frame.origin.y += 20.0f;
    [view setFrame:frame];
    [self.window addSubview:nav.view];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    
    UINavigationBar *morenavbar = navigationController.navigationBar;
    UINavigationItem *morenavitem = morenavbar.topItem;
    /* We don't need Edit button in More screen. */
    morenavitem.rightBarButtonItem = nil;
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
